﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualBasic;
using Blacklight.Controls;
using MyStudioServerMp3UploaderPlayer.MyStudioWs;
using System.ServiceModel;
using DC.FileUpload;
using System.Text;

namespace MyStudioServerMp3UploaderPlayer
{
    public partial class MainPage : UserControl
    {
        #region Global Variables

        public int intUserID;
        public string strPassword;
        public int intSongID;
        public string strSongPath;
        public string strSongOriginalName;

        #endregion

        MediaPlayer media = new MediaPlayer();
        ConfirmDelete objConfirmDelete = new ConfirmDelete();
        btnDeleteControl objDeleteControl = new btnDeleteControl();
        FileUploadControl objUpload = new FileUploadControl();

        #region MainPage
        public MainPage (string UserID, string Password, string SongID)
        {
            InitializeComponent();

            Main.Visibility = Visibility.Collapsed;

            intUserID = Convert.ToInt32(UserID);
            strPassword = Password;
            intSongID = Convert.ToInt32(SongID);

            objDeleteControl.Click += new RoutedEventHandler(objDeleteControl_Click);
            objConfirmDelete.Click += new RoutedEventHandler(objConfirmDelete_Click);

            Main.Children.Add(media);
            Main.Children.Add(objConfirmDelete);
            Main.Children.Add(objUpload);
            Main.Children.Add(objDeleteControl);

            GetSongInformation();
        }
        #endregion

        #region GetSongInformation
        private void GetSongInformation()
        {
            // *** Call a Web Service to get the File path and the Original Name
            WebServiceSoapClient MyStudioWs = new WebServiceSoapClient();
            EndpointAddress WebServiceEndpointAddress =
                new EndpointAddress(string.Format(@"{0}/Webservice/{1}", GetWebserviceAddress(), "WebService.asmx"));
            MyStudioWs.Endpoint.Address = WebServiceEndpointAddress;
            MyStudioWs.GetSongInformationCompleted += new EventHandler<GetSongInformationCompletedEventArgs>(MyStudioWs_GetSongInformationCompleted);
            MyStudioWs.GetSongInformationAsync(intUserID, intSongID);
        }

        void MyStudioWs_GetSongInformationCompleted(object sender, GetSongInformationCompletedEventArgs e)
        {
            try
            {
                // Show Song
                SongInformation SongInformation = (SongInformation)e.Result;
                strSongPath = SongInformation.SongFileDirectory;
                strSongOriginalName = SongInformation.SongOriginalFileName.Replace(".mp3", ""); ;

                if (strSongOriginalName.Length > 12)
                {
                    strSongOriginalName = Strings.Left(strSongOriginalName, 10) + "...";
                }

                media.SongName = strSongOriginalName;
                media.MediaSourceUri = new Uri(strSongPath, UriKind.Absolute);

                Main.Visibility = Visibility.Visible;
                media.Visibility = Visibility.Visible;
                objDeleteControl.Visibility = Visibility.Visible;
                objConfirmDelete.Visibility = Visibility.Collapsed;
                objUpload.Visibility = Visibility.Collapsed;
            }
            catch
            {
                ShowUploadControl();
            }
        }
        #endregion

        #region objUpload_UploadComplete
        void objUpload_UploadComplete(object sender, RoutedEventArgs e)
        {
            GetSongInformation();
        }
        #endregion

        #region GetWebserviceAddress
        private string GetWebserviceAddress()
        {
            string strXapFile = @"/ClientBin/MyStudioServerMp3UploaderPlayer.xap";
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri.Replace(strXapFile, "");
            return strBaseWebAddress;
        }
        #endregion

        #region objDeleteControl_Click
        void objDeleteControl_Click(object sender, RoutedEventArgs e)
        {
            if (media.Visibility == Visibility.Visible)
            {
                // Set to Confirm delete
                media.Visibility = Visibility.Collapsed;
                objConfirmDelete.Visibility = Visibility.Visible;
                Main.UpdateLayout();
            }
            else
            {
                // Delete Mp3
                media.SongName = "";
                media.MediaSourceUri = null;

                WebServiceSoapClient MyStudioWs = new WebServiceSoapClient();
                EndpointAddress WebServiceEndpointAddress =
                    new EndpointAddress(string.Format(@"{0}/Webservice/{1}", GetWebserviceAddress(), "WebService.asmx"));
                MyStudioWs.Endpoint.Address = WebServiceEndpointAddress;
                MyStudioWs.DeleteMp3FileAsync(intUserID, strPassword, intSongID);

                ShowUploadControl();
            }
        }
        #endregion

        #region objConfirmDelete_Click
        void objConfirmDelete_Click(object sender, RoutedEventArgs e)
        {
            media.Visibility = Visibility.Visible;
            objConfirmDelete.Visibility = Visibility.Collapsed;
            Main.UpdateLayout();
        }
        #endregion

        #region ShowUploadControl
        private void ShowUploadControl()
        {
            // Show Upload Control
            media.Visibility = Visibility.Collapsed;
            objDeleteControl.Visibility = Visibility.Collapsed;
            objConfirmDelete.Visibility = Visibility.Collapsed;
            objUpload.Visibility = Visibility.Visible;
            objUpload.SetToSelectFileMode();
            Main.Visibility = Visibility.Visible;

            // ** Values passed in the Header
            // u = UserId
            // p = password
            // s = SongID

            StringBuilder AdditionalParamaters = new StringBuilder();
            AdditionalParamaters.Append("?");
            AdditionalParamaters.Append("&u=" + intUserID.ToString());
            AdditionalParamaters.Append("&s=" + intSongID.ToString());
            AdditionalParamaters.Append("&p=" + strPassword);

            Uri uri = new Uri(GetWebserviceAddress() + @"/FileUpload.ashx" + AdditionalParamaters.ToString(), UriKind.Absolute);
            objUpload.UploadUrl = uri;
            objUpload.UploadChunkSize = 4194304;
            objUpload.ResizeImage = false;
            objUpload.files.Clear();

            objUpload.UploadComplete += new RoutedEventHandler(objUpload_UploadComplete);
        }
        #endregion
    }
}
